<?php

namespace App\Http\Controllers\Api;

use App\Models\Customer;
use App\Services\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class LoginController extends Controller
{
    /**
     * 手机号密码登录
     */
    public function index(Request $request)
    {
        $data = $request->all([
            'mobile', 'password'
        ]);

        $user = Customer::query()->where('mobile', $data['mobile'])->first();

        if (!$user) {
            return Api::msg('api.login.user_not_found');
        }

        if (!Hash::check(password('password'), $user->password)) {
            return Api::msg('api.login.check_failure');
        }

        return Api::msg('api.login.success', Api::token($user));
    }

    /**
     * 登出
     */
    public function logout()
    {
        if (Auth::guard('api')->check()) {
            Auth::guard('api')->user()->token()->delete();
        }
        return Api::msg('api.logout.success');
    }
}
